import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/base/base_entity.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/me/entity/userEntity.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/studentsReview/entity/studentReviewEntity.dart';
import 'package:school_safe_teacher/utils/sp_utils.dart';

import 'action.dart';
import 'state.dart';

Effect<studentsReviewState> buildEffect() {
  return combineEffects(<Object, Effect<studentsReviewState>>{

    Lifecycle.initState: _init,
    studentsReviewAction.onrefresh: _onrefresh,
    studentsReviewAction.onload: _onload,
    studentsReviewAction.onreview: _onreview,
  });
}

void _onrefresh(Action action, Context<studentsReviewState> ctx) {
  if (ctx.state.type == 0) {
    _request(ctx, 1);
  } else
    _requestrResidentStudentList(ctx, 1);
}

void _onload(Action action, Context<studentsReviewState> ctx) {
  if (ctx.state.type == 0) {
    _request(ctx, ctx.state.page);
  } else
    _requestrResidentStudentList(ctx, ctx.state.page);
}

void _init(Action action, Context<studentsReviewState> ctx) {
  _onrefresh(action, ctx);
}

void _onreview(Action action, Context<studentsReviewState> ctx) {
  _requestOnreview(ctx,action.payload["type"],action.payload["memberId"]);
}

Future<void> _request(Context<studentsReviewState> ctx, int page) async {
  Map<String, String> params = Map();
  params["page_size"] = Constants.PAGE_SIZE.toString();
  params["page"] = page.toString();
  params["class_id"] = await SpUtil.getStringItem(Constants.classID);
  DioApi.instance.asyncRequestNetwork<studentReviewEntity>(
      Method.post, HttpApi.STUDENT_REVIEW_LIST, ctx.context,params: params,
      onStart: () {},
      onSuccessList: (List<dynamic> list) async {
        List<studentReviewEntity> newsList = [];
        list.forEach((element) {
          newsList.add(EntityFactory.generateOBJ(element));
        });
        ctx.dispatch(studentsReviewActionCreator.onSuccess(newsList));
      },
      onError: (String msg) {
        showToast(msg);
        ctx.state.easyRefreshController.finishRefresh();
        ctx.state.easyRefreshController.finishLoad();
      },
      onDone: () {
        ctx.dispatch(studentsReviewActionCreator.onComplete());
      },
      isList: true);
}

Future<void> _requestrResidentStudentList(Context<studentsReviewState> ctx,
    int page) async {
  Map<String, String> params = Map();
  params["page_size"] = Constants.PAGE_SIZE.toString();
  params["page"] = page.toString();
  params["class_id"] = await SpUtil.getStringItem(Constants.classID);
  DioApi.instance.asyncRequestNetwork<studentReviewEntity>(
      Method.post, HttpApi.RESIDENTSTUDENTLIST, ctx.context,params: params,
      onStart: () {},
      onSuccessList: (List<dynamic> list) async {
        List<studentReviewEntity> newsList = [];
        list.forEach((element) {
          newsList.add(EntityFactory.generateOBJ(element));
        });
        ctx.dispatch(studentsReviewActionCreator.onSuccess(newsList));
      },
      onError: (String msg) {
        ctx.state.easyRefreshController.finishRefresh();
        ctx.state.easyRefreshController.finishLoad();
        showToast(msg);
      },
      onDone: () {
        ctx.dispatch(studentsReviewActionCreator.onComplete());
      },
      isList: true);
}


Future<void> _requestOnreview(Context<studentsReviewState> ctx, int type,
    String member_id) async {
  Map<String, dynamic> params = Map();
  params["status"] = type;
  params["member_id"] = member_id;
  EasyLoading.show();
  DioApi.instance.asyncRequestNetwork<BaseEntity>(
      Method.post, HttpApi.EXMINEMEMBER, ctx.context,params: params,
      onStart: () {},
      onSuccess: ( dynamic t) async {
       showToast("审核成功");
  ctx.state.easyRefreshController.callRefresh();
      },
      onError: (String msg) {
        showToast(msg);
      },
      onDone: () {
        EasyLoading.dismiss();
      },
      isList: false);
}